
/*
*  StoreFilter.cpp
*
*	Copyright (C) 2012
*	Crane <szcshui@gmail.com>
*	All Rights Reserved
*
*************************************************************************
*
*  Description:
*      This file implements Store db all index files bloom filter 
*		Support two keys.
*
*/

#include <string>
#include <sstream>
#include "FilterMap.h"
#include "Utils.h"

using namespace std;

#ifdef _DEBUG

int TestBloom()
{
	BloomFilter filter(4096, 0.00002);

	for( int i = 31076200; i < 31077201; i++)
	{
		std::stringstream oss;
		oss << i;

		string str(oss.str());
		filter.Insert(str);
	}

	cout << filter.ToString()  << endl;

	int j = 0;
	for( int i = 31076200; i < 31087201; i++)
	{
		std::stringstream oss;
		oss << i;

		string str(oss.str());
		filter.Insert(str);
		
		if(filter.IsPresent(str))
		{
			cout << i << endl;
			if (i >= 31077201)
			{
				j++;
			}
		}
	}

	cout << "No of falses: " << j << endl;

	cout << "Completed" << endl;

	return 0;
}

#endif


bool ParseInputArguments(FilterMap& filter, const string& cmdLine)
{
	if (cmdLine == "exit" || cmdLine == "quit")
	{
		return false;
	}
	else
	{
		int pos = -1;
		if ( (pos = cmdLine.find("account=")) == 0)
		{
			pos = cmdLine.find("=");
			string key = cmdLine.substr(pos + 1);
			filter.Search(key);
		}
		else if ( (pos = cmdLine.find("account in")) == 0)
		{
			pos = cmdLine.find("in");
			string key = cmdLine.substr(pos + 2);
			filter.SearchWithList(key);
		}
		else
		{
			cout << "Usage:" << endl;
			cout << "\t account=[key]" << endl;
			cout << "\t account in [file]" << endl;
		}

		return true;
	}
}

//
//	Test Data:
//		1000 index files,
//		30 856 029 accounts
//
int main()
{
	//assume average accounts in each in index files
	const int BitArrayLength = 1024; 

	//only for unit test
	//return TestBloom();
	
	const string path = GetExecuteFilePath() + "/data/";

	FilterMap filterMapAccount(BitArrayLength, true, path + "AccountBloom.flt");
	//FilterMap filterMapAccountSec(4096, false, path + "AccountSecurityBloom.flt");

	//filterMapAccount.RebuildBloomFilterFromPath(path, 1, 1000);
	//filterMapAccountSec.RebuildBloomFilterFromPath(path, 1, 1000);

	/*string cmdLine;
	do
	{
		getline(std::cin, cmdLine, '\n');
	}while(ParseInputArguments(filterMapAccount, cmdLine));*/

	
	//
	//seach single account
	cout << "only for test ... " << endl;
	filterMapAccount.Search("31076201");
	filterMapAccount.Search("30778068");

	//only for unit test
	//filterMapAccountSec.Search("30359913:0P000003DH");
	//filterMapAccountSec.Search("30778068:FOUSA00EHO");

	//search in account list
	filterMapAccount.SearchWithList(path + "account-20k.lst");

	cout << "Press any key to exit." << endl;
	getchar();
}